<style type="text/css">
.grid-view .status-column { width: 5%; }
.grid-view .filter-datepicker input { width: 100px; }
</style>

<?php
$this->breadcrumbs = array(
    'Documents' => array('index'),
    'Manage',
);

$this->menu = array(
    array('label' => 'Create Document', 'url' => array('create')),
);
?>

<h1>Manage Documents</h1>

<div>
    <span>Select a language : </span>
    <?php echo CHtml::dropDownList('locale',$selectedLocale,$langs); ?>
</div>

<?php $grid = $this->widget('zii.widgets.grid.CGridView', array(
    'id' => 'document-grid',
    'dataProvider' => $model->search($selectedLocale),
    'filter' => $model,
    'selectableRows' => 2,
    'selectionChanged' => "updateSelectors",
    'columns' => array(
        array(
            'class' => 'CCheckBoxColumn',
            'value' => '$data->id',
            'htmlOptions' => array('width' => '3%','style' => 'text-align: center'),
        ),
        array(
            'name' => 'title',
            'type' => 'raw',
            'value' => 'CHtml::link($data->localeDocument->title,url("/Site/admin/document/update",array("id"=>$data->id)))',
            'htmlOptions' => array('width' => '10%'),
        ),
        array(
            'name' => 'category_id',
            'sortable' => false,
            'type' => 'raw',
            'class'=>'Site.components.CategoryDropDownListColumn',
            'rootId'=>SiteSettings::ROOT_DOCUMENT_CATEGORY_ID,
            'locale'=>$selectedLocale,
            'value' => '$data->getLanguageCategorySection("'.$selectedLocale.'")',
            'htmlOptions' => array('width' => '10%','style' => 'text-align: center'),
        ),
        array(
            'name' => 'status',
            'type' => 'raw',
            'value' => 'CHtml::link($data->statusText,"#",array("id"=>$data->id,"class"=>($data->status==1 ? "active" : "")))',
            'filter' => $model->getStatusOptions(),
            'htmlOptions' => array(
                'class' => 'status-column',
                'width' => '10%',
            ),
        ),
        array(
            'header' => 'Date created (dd/mm/yyyy)',
            'name' => 'dateCreated',
            'class'=>'Core.components.CDatePickerColumn',
            'value' => 'date("d/m/Y",strtotime($data->create_time))',
            'fromOptions' => array(
                'maxDate' => ''
            ),
            'toOptions' => array(
                'maxDate' => ''
            ),
            'htmlOptions'=>array(
                'width'=>'8%',
                'style' => 'text-align: center',
            ),
        ),
        array(
            'header' => 'Download',
            'type' => 'raw',
            'value' => 'CHtml::link("Download",url("/Site/admin/document/download",array("id"=>$data->id)))',
            'htmlOptions' => array('width' => '5%','style' => 'text-align: center'),
        ),
        array(
            'class' => 'CButtonColumn',
            'template' => '{update} {delete}',
            'htmlOptions' => array(
                'width' => '3%',
                'style' => 'text-align: center',
            ),
        ),
    ),
));

if ($grid->dataProvider->ItemCount) {
    $this->menu[] = array('label' => 'Delete selected items', 'url' => $this->createUrl('delete'), 'linkOptions' => array('onclick' => 'return multipleDelete("document-grid",this.href)'));
}
Yii::app()->clientScript->registerScriptFile(Yii::app()->core->AssetUrl . '/scripts/gridview.js', CClientScript::POS_BEGIN);
?>

<?php
$script = '
    $(".grid-view .status-column a").live("click", function(){
        status = $(this).attr("class")==""?"inactive": $(this).attr("class");
        $.ajax({
            url:"' . serviceUrl('Site.DocumentAPI.setStatus', 'ajax') . '",
            type: "get",
            data: {"status": status, "id": $(this).attr("id")},
            success: function(){
                $.fn.yiiGridView.update("document-grid");
            }
        });
        return false;
    })
';
cs()->registerScript('set-status', $script, CClientScript::POS_READY);
?>

<script type="text/javascript">
$('#locale').change(function(){
    window.location.href = '<?php echo $this->createUrl('/Site/admin/document/admin'); ?>&locale='+$(this).val();        
});
</script>